/*
 * This program is non-free software, but you can redistribute it and/or modify
 * it if you are in hjj(Huangshi Hejianjiao) team.
 */
package com.truthbean.jutils.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

/**
 *
 * @author Administrator
 */
public class ValidateUtil {

    public static <T> Map<String, String> getValidateMsg(T t) {
        Map<String, String> map = new HashMap<>();
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<T>> validate = validator.validate(t);

        validate.stream().map((cv) -> {
            map.put(cv.getPropertyPath() + "", cv.getMessage());
            return cv;
        }).forEach((cv) -> {
            System.out.println(cv.getMessage() + "(" + cv.getPropertyPath() + ")" + "-->" + cv.getInvalidValue());
        });
        return map;
    }
}
